/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.miscellaneous.io;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import phoside.util.IOUtil;

/**
 *
 * @author gjj
 */
public class PhosphoPepExtractorLocal {
    private String baseUrl;

    public PhosphoPepExtractorLocal(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public void download(String dirTo) throws IOException {
        List<String> strOut = new ArrayList();

        List<String> files = IOUtil.listFilesInFolder(baseUrl);
        int n = files.size();
//        n=20;
        for (int i=0; i<n; i++) {
            if (i%100==0)
                System.out.println(String.format("%5d/%-5d", i, n));

            String file = files.get(i);
            if (!file.startsWith("peptideSearch")||!file.endsWith(".cgi"))
                continue;

            List<String> lines = IOUtil.readStringListAscii(baseUrl+"\\"+file);
            
            String acc = getAcc(lines);

            strOut.add(">"+acc+"\t"+file);

            List<String[]> peps = getPeptides(lines);
            for (String[] pep : peps) {
                strOut.add(pep[0]+"\t"+pep[1]+"\t"+pep[2]);
            }
        }

        IOUtil.writeCollectionAscii(strOut, dirTo);
    }

    private String getAcc(List<String> lines) {
        // uniprot
        //String acc = lines.get(1021);
        //acc = acc.replaceFirst("<tr><td nowrap=\"1\" class=\"rev_gray_head\">Swiss Prot ID</td> <td><a href='[^>]+'>", "");

        // sgd
        String acc = lines.get(992);
        acc = acc.replaceFirst("<tr><td class=\"rev_gray_head\">ID</td> <td nowrap=\"1\"><A HREF='[^>]+>", "");

        int idx = acc.indexOf('<');
        acc = acc.substring(0, idx).trim();
        return acc;
    }

    private List<String[]> getPeptides(List<String> lines) throws IOException {
        String line = lines.get(1031);
        line = line.replace("<td>", "@");
        line = line.replace("</td>", "@");
        List<String[]> peps = new ArrayList();

        Pattern p = Pattern.compile("@([^@]*<SPAN class=[^>]*phospho NAME=[^>]*phospho ID=[^>]*phospho>.\\0052</SPAN>[^@]*)@[^<]*" +
                "<td align=\"right\">([^@]+)@[^@]+@[^@]+@[^<]*<td align=\"right\">([^@]+)@");
        Matcher m = p.matcher(line);
        while (m.find()) {
            String pep = m.group(1).toUpperCase();
            pep = pep.replaceAll("<SPAN CLASS=PHOSPHO NAME=PHOSPHO ID=PHOSPHO>(.)\\052</SPAN>", "p$1");
            pep = pep.replaceAll("<SPAN CLASS=AMBIPHOSPHO NAME=AMBIPHOSPHO ID=AMBIPHOSPHO>(.)\\0052</SPAN>", "a$1");
//            int s = pep.indexOf('.');
//            int t = pep.indexOf('.', s+1);
//            pep = pep.substring(s+1, t);
            String score = m.group(2);
            String deltacn = m.group(3);

            peps.add(new String[]{pep,score,deltacn});
        }

        return peps;
    }

    public static void main(String[] args) {

        // fly
        String baseUrl = "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphopep\\PhosphoPep-fly";
        PhosphoPepExtractorLocal downloaderfly = new PhosphoPepExtractorLocal(baseUrl);
        String dirPep = "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphopep\\PhosphoPep-fly-10022009.pep";

//        try {
//            downloaderfly.download(dirPep);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        // worm
//        baseUrl = "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphopep\\PhosphoPep-worm";
//        PhosphoPepDownloader1 downloader = new PhosphoPepDownloader1(baseUrl);
//        dirPep = "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphopep\\PhosphoPep-worm-10022009.pep";
//
//        try {
//            downloader.download(dirPep);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        // yeast
        baseUrl = "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphopep\\PhosphoPep-yeast";
        PhosphoPepExtractorLocal downloader1 = new PhosphoPepExtractorLocal(baseUrl);
        String dirPep1 = "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphopep\\PhosphoPep-yeast-10022009.pep";

        try {
            downloader1.download(dirPep1);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        // human
//        baseUrl = "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphopep\\PhosphoPep-human";
//        PhosphoPepDownloader1 downloader2 = new PhosphoPepDownloader1(baseUrl);
//        String dirPep2 = "D:\\projects\\PHOSIDE\\PHOSIDE\\data\\phosphopep\\PhosphoPep-human-10022009.pep";
//
//        try {
//            downloader2.download(dirPep2);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }



}
